package com.ygl.strategy.sentinel.nacos.loader;

import com.alibaba.csp.sentinel.datasource.ReadableDataSource;
import com.alibaba.csp.sentinel.datasource.nacos.NacosDataSource;
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule;
import com.alibaba.csp.sentinel.slots.system.SystemRule;
import com.ygl.common.nacos.configuration.NacosAutoConfiguration;
import com.ygl.common.nacos.constant.NacosConstant;
import com.ygl.strategy.sentinel.constant.SentinelStrategyConstant;
import com.ygl.strategy.sentinel.loader.SentinelDataSourceRuleLoader;

import java.util.List;
import java.util.Properties;

/**
 * @Author yanggld
 * @Date 2020/1/10-11:18
 * @Description
 */
public class SentinelNacosRuleLoader extends SentinelDataSourceRuleLoader {

	private Properties properties;

	@Override
	public void initialize() {
		properties = NacosAutoConfiguration.createNacosProperties(applicationContext.getEnvironment());
	}

	@Override
	public ReadableDataSource<String, List<FlowRule>> getFlowRuleDataSource() {
		return new NacosDataSource<>(properties, codeAdapter.getGroup(), codeAdapter.getServiceId() + "-" + SentinelStrategyConstant.SENTINEL_FLOW_KEY, sentinelFlowRuleParser);
	}

	@Override
	public ReadableDataSource<String, List<DegradeRule>> getDegradeRuleDataSource() {
		return new NacosDataSource<>(properties, codeAdapter.getGroup(), codeAdapter.getServiceId() + "-" + SentinelStrategyConstant.SENTINEL_DEGRADE_KEY, sentinelDegradeRuleParser);
	}

	@Override
	public ReadableDataSource<String, List<AuthorityRule>> getAuthorityRuleDataSource() {
		return new NacosDataSource<>(properties, codeAdapter.getGroup(), codeAdapter.getServiceId() + "-" + SentinelStrategyConstant.SENTINEL_AUTHORITY_KEY, sentinelAuthorityRuleParser);
	}

	@Override
	public ReadableDataSource<String, List<SystemRule>> getSystemRuleDataSource() {
		return new NacosDataSource<>(properties, codeAdapter.getGroup(), codeAdapter.getServiceId() + "-" + SentinelStrategyConstant.SENTINEL_SYSTEM_KEY, sentinelSystemRuleParser);
	}

	@Override
	public ReadableDataSource<String, List<ParamFlowRule>> getParamFlowRuleDataSource() {
		return new NacosDataSource<>(properties, codeAdapter.getGroup(), codeAdapter.getServiceId() + "-" + SentinelStrategyConstant.SENTINEL_PARAM_FLOW_KEY, sentinelParamFlowRuleParser);
	}

	@Override
	public String getConfigType() {
		return NacosConstant.NACOS_TYPE;
	}
}
